﻿using System.Text.RegularExpressions;
using K9Nano.Exceptions;

namespace K9Nano.Reporting;

public static class ReportUtils
{
    private const string FieldPattern = @"(?<field>@\w+)";

    /// 清空 <see cref="Fileds"/> 中原有的字段，并从 <see cref="SqlText"/> 重新解析字段
    /// </summary>
    /// <exception cref="FriendlyException">解析失败</exception>
    public static IReadOnlySet<string> ParseFields(string sqlText)
    {
        var fields = new HashSet<string>();

        var matches = Regex.Matches(sqlText, FieldPattern);
        foreach (Match match in matches)
        {
            if (fields.Contains(match.Value))
            {
                throw new FriendlyException("报表字段不能重复：" + match.Value);
            }
            fields.Add(match.Value);
        }

        return fields;
    }

}
